﻿using SilenthinkContract;
using Newtonsoft.Json.Linq;
using SilenthinkContract.Domain;

namespace Web.OAuth.Client.Impl
{
    public class QqClient:OAuthClient
    {
        public QqClient(OAuthConfig configuration) : base(configuration)
        {
        }

        #region Overrides of OAuthClient

        /// <summary>
        /// Should return parsed <see cref="ExternalUser"/> using content received from provider.
        /// </summary>
        /// <param name="content">The content which is received from provider.</param>
        protected override ExternalUser ParseUserInfo(string content)
        {
            var lpos = content.IndexOf('(');
            var rpos = content.IndexOf(')');
            var response = content.Substring(lpos + 1, rpos - lpos - 1);
            var result = JObject.Parse(response);
            return new ExternalUser { Id = result["openid"].Value<string>(), ProviderName = ProviderName };
        }

        protected override void BeforeGetUserInfo(RestSharp.IRestRequest request)
        {
            request.AddParameter(AccessTokenKey, AccessToken);
        }
        #endregion
    }
}